<?php
/**
* Simple html table.
* TODO rename to table
* FIXME not yet tested
*/
class htmltable extends elementInner
{
	public $tableData;
	public $tdClass;
	public $trClass;

    public function __construct($id=NULL, $tableData=NULL, $class=NULL, $label=NULL)
    {
        parent::elementInner('table',$id, NULL, $class, $label);
        //var_dump($tableData);
        $this->tableData = $tableData;
    }


    /**
     * Set the css class of td
     *
     * @param string $tdClass
     */
    public function setTdClass($tdClass)
    {
    	$this->tdClass = $tdClass;
    }

    /**
     * Set the css class of tr
     *
     * @param string $trClass
     */
    public function setTrClass($trClass)
    {
        $this->trClass = $trClass;
    }

    public function addLine($lineData)
    {
    	if ( is_array($lineData) )
    	{
            $this->tableData[] = $lineData;
    	}
    }

    public function setLine($line, $lineData)
    {
        if ( is_array($lineData) )
        {
            $this->tableData[$line] = $lineData;
        }
    }

    public function setCellValue($line, $row, $value)
    {
        $this->tableData[$line][$row] = $value;
    }

    public function toStringInner()
    {
        
        $tableData = $this->tableData;

        //threat as a multidensional array
        if (is_array($tableData) && is_array($tableData[0]))
        {
            foreach ($tableData as $line => $info)
            {
                foreach ($info as $column => $value)
                {
                    var_dump($column);
                    var_dump($value);
                    $tr[$line] = new Td($line.'_'.$column, $value, $this->tdClass);
                }

                $tr[$line] = new Tr($line, $tr[$line],$this->trClass);
            }
            $this->inner = $tr;
        }
        else
        {
            $this->inner = $this->tableData;
        }
    }

    public function __toString()
    {
    	$this->toStringInner();
        unset($this->tableData);
        return parent::__toString();
    }


    /*class simpleTable extends htmlTable
    {
        function simpleTable($id=NULL, $data=NULL, $class=NULL)
        {
            foreach ($data as $line => $info)
            {
                if ($line)
                {
                    $tdA[$line] = new td(NULL, $line);
                }
                if ($info)
                {
                    $tdB[$line] = new td(NULL, $info);
                }
                $tr[$line] = new tr(NULL, array($tdA[$line], $tdB[$line]));
                $inner[]   = $tr[$line];
            }
            parent::htmlTable($id, $inner, $class);
        }
    }*/

}
?>
